<?php

namespace common\models;

use common\cache\DictCache;
use common\helpers\commonApi;
use common\helpers\PageApi;
use Yii;

/**
 * This is the model class for table "{{%user_coupon}}".
 *
 * @property int $id
 * @property int|null $coupon_id 卡券id
 * @property int|null $userid 用户id
 * @property float|null $discount_amount 优惠金额
 * @property int|null $use 使用
 * @property int|null $status 状态
 * @property int|null $delete 删除
 * @property string|null $coupon_info 卡券信息
 * @property string|null $create_time 创建时间
 * @property string|null $update_time 修改时间
 * @property string|null $use_time 使用时间
 * @property string|null $order_sn 订单号
 * @property int|null $campus_id 校区id
 */
class UserCoupon extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%user_coupon}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['coupon_id', 'userid', 'use', 'status', 'delete','campus_id'], 'integer'],
            [['discount_amount'], 'number'],
            [['coupon_info'], 'string'],
            [['create_time', 'update_time', 'use_time'], 'safe'],
            [['order_sn'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'coupon_id' => '卡券id',
            'userid' => '用户id',
            'discount_amount' => '优惠金额',
            'use' => '使用',
            'status' => '状态',
            'delete' => '删除',
            'coupon_info' => '卡券信息',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
            'use_time' => '使用时间',
            'order_sn' => '订单号',
            'campus_id' => '校区id',
        ];
    }

    /**
     * 我的
     * @param $userid
     * @param $page
     * @param $campus_id
     * @return array
     */
    public function getUserList($userid,$page,$campus_id,$use,$is_buy)
    {
        $query = self::find()
            ->where(['userid'=>$userid,'status'=>1,'delete'=>0])
            ->andFilterWhere(['use'=>$use])
            ->select(['id','discount_amount','coupon_info','create_time','use'])
            ->orderBy(['discount_amount'=>SORT_DESC,'create_time'=>SORT_DESC]);
        if ($is_buy){
            $query->andWhere(['or',['campus_id'=>0],['campus_id'=>$campus_id]]);
        }
        $data = PageApi::apiPage($query,$page);
        $arr = [];
        $user_coupon_use = DictCache::get('user_coupon_use');
        $campus_ids = array_column($data['data'],'campus_id');
        $campus = (new Campus())->getCampusName($campus_ids);
        $coupon = new Coupon();
        foreach ($data['data'] as $datum){
            $coupon_info = json_decode($datum['coupon_info'],true);
            $one = $coupon->oneData(0,[],[],0,$campus,$coupon_info);
            $one['uc_id'] = $datum['id'];
            $one['discount_amount'] = $datum['discount_amount'];
            $one['status'] = $datum['use']+2;
            $one['status_title'] = $user_coupon_use[$datum['use']]??'';
            $arr[] = $one;
        }
        $data['data'] = $arr;
        return commonApi::message('ok',true,$data);
    }
}
